/**
 * 
 */
package ar.com.jsejtman.test.common;

import ar.com.jsejtman.common.security.credential.service.ISecurityCredentialService;
import ar.com.jsejtman.common.security.schema.entity.SecurityUser;
import ar.com.jsejtman.common.security.schema.service.ISecurityUserService;
import ar.com.jsejtman.common.service.exception.EntitySavingException;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

/**
 * Inicializador de datos.
 * 
 * @author jsejtman
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@Transactional
@TransactionConfiguration(transactionManager = "txManager", defaultRollback = false)
public class Initializer {

	/**
	 * Servicio de seguridad.
	 */
	@Autowired
	private ISecurityUserService securityUserService;

	/**
	 * Credential service.
	 */
	@Autowired
	private ISecurityCredentialService securityCredentialService;

	/**
	 * Cargador de datos.
	 */
	@Test
	public final void cargarDatosUsuarios() {

		final SecurityUser su = new SecurityUser();
		su.setEmailAddress("sejtman@yahoo.com.ar");
		su.setFirstName("Julio");
		su.setLastName("Sejtman");
		su.setUsername("julio");
		su.setPassword("julio");
		try {
			this.securityUserService.saveSecurityUser(su);

			final UserDetails usr = this.securityCredentialService.loadUserByUsername("julio");

			TestCase.assertEquals(su.getUsername(), usr.getUsername());
			TestCase.assertEquals(su.getPassword(), usr.getPassword());

		} catch (final EntitySavingException e) {
			e.printStackTrace();
			TestCase.fail(e.getMessage());
		}

	}

}
